import json
import os

import wget
from loguru import logger

from linked_care_client import LinkedCareClient

# 读登录信息
if os.path.exists('web.json'):
    login = json.load(open('web.json', 'r'))

linked_care_client = LinkedCareClient(login)

# 读患者列表
for line in open('患者列表.txt', encoding='utf-8'):
    line = line.replace('\n', '')
    logger.info('处理患者【%s】开始......' % line)
    _items, _is_end = linked_care_client.search_text(line)

    for item in _items:
        images = linked_care_client.search_with_images(item['id'])
        images_download_url = linked_care_client.image_export(item['name'], item['privateId'], images)
        if images_download_url:
            file_name = wget.download(images_download_url)
            logger.info('下载: %s' % file_name)
        else:
            logger.info('打包异常')
